home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / OS⁄Toolbox / QuickDraw / RlePict / lib / rle_row_alc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  3.7 KB  |  149 lines  |  [TEXT/KAHL]

  1. /*
  2.  * This software is copyrighted as noted below.  It may be freely copied,
  3.  * modified, and redistributed, provided that the copyright notice is 
  4.  * preserved on all copies.
  5.  * 
  6.  * There is no warranty or other guarantee of fitness for this software,
  7.  * it is provided solely "as is".  Bug reports or fixes may be sent
  8.  * to the author, who may or may not act on them as he desires.
  9.  *
  10.  * You may not include this software in a program or other software product
  11.  * without supplying the source, or without informing the end-user that the 
  12.  * source is available for no extra charge.
  13.  *
  14.  * If you modify this software, you should include a notice giving the
  15.  * name of the person performing the modification, the date of modification,
  16.  * and the reason for such modification.
  17.  *
  18.  *  Modified at BRL 16-May-88 by Mike Muuss to avoid Alliant STDC desire
  19.  *  to have all "void" functions so declared.
  20.  */
  21. /* 
  22.  * rle_row_alc.c - Allocate buffers for rle_getrow/rle_putrow.
  23.  * 
  24.  * Author:    Spencer W. Thomas
  25.  *         Computer Science Dept.
  26.  *         University of Utah
  27.  * Date:    Fri Nov 14 1986
  28.  * Copyright (c) 1986, Spencer W. Thomas
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include "rle.h"
  33.  
  34. #ifdef USE_STDLIB_H
  35. #include <stdlib.h>
  36. #else
  37.  
  38. #ifdef USE_STRING_H
  39. #include <string.h>
  40. #else
  41. #include <strings.h>
  42. #endif
  43.  
  44. #ifndef VOID_STAR
  45. extern char * malloc();
  46. #else
  47. extern void *malloc();
  48. #endif
  49. extern void free();
  50.  
  51. #endif /* USE_STDLIB_H */
  52.  
  53. /*****************************************************************
  54.  * TAG( rle_row_alloc )
  55.  * 
  56.  * Allocate buffer space for use by rle_getrow and rle_putrow.
  57.  * Inputs:
  58.  *     the_hdr:    Header structure for RLE file to be read or
  59.  *            written.
  60.  * Outputs:
  61.  *    scanp:        Pointer to pointer to created scanline buffer.
  62.  *            This pointer is adjusted for the alpha channel,
  63.  *            if present.
  64.  *    Returns 0 for success, -1 if malloc failed.
  65.  * Assumptions:
  66.  *     No input scanline will extend beyond the declared xmax endpoint.
  67.  * Algorithm:
  68.  *    Count number of channels actually used (check bitmap).
  69.  *     Allocate nchan*rowlength pixels, allocate a buffer
  70.  *    to hold ncolors+alpha pointers, and give each channel
  71.  *    rowlength pixels.  Rowlength is xmax + 1, to allow for rle_getrow
  72.  *    usage.
  73.  */
  74. int
  75. rle_row_alloc( the_hdr, scanp )
  76. rle_hdr *the_hdr;
  77. rle_pixel ***scanp;
  78. {
  79.     rle_pixel ** scanbuf, * pixbuf;
  80.     int rowlen, nchan = 0, i, ncol;
  81.  
  82.     rowlen = the_hdr->xmax + 1;
  83.     if ( the_hdr->alpha && RLE_BIT( *the_hdr, RLE_ALPHA ) )
  84.     nchan++;
  85.     for ( i = 0; i < the_hdr->ncolors; i++ )
  86.     if ( RLE_BIT( *the_hdr, i ) )
  87.          nchan++;
  88.  
  89.     ncol = the_hdr->ncolors + the_hdr->alpha;
  90.  
  91.     if ( (scanbuf = (rle_pixel **)malloc( ncol * sizeof(rle_pixel *) )) == 0 )
  92.     return -1;
  93.     if ( (pixbuf = (rle_pixel *)malloc( nchan * rowlen *
  94.                        sizeof(rle_pixel) )) == 0 )
  95.     {
  96.     free( scanbuf );
  97.     return -1;
  98.     }
  99.  
  100.     if ( the_hdr->alpha )
  101.     scanbuf++;
  102.  
  103.     for ( i = -the_hdr->alpha; i < the_hdr->ncolors; i++ )
  104.     if ( RLE_BIT( *the_hdr, i ) )
  105.     {
  106.         scanbuf[i] = pixbuf;
  107.         pixbuf += rowlen;
  108.     }
  109.     else
  110.         scanbuf[i] = 0;
  111.     *scanp = scanbuf;
  112.  
  113.     return 0;
  114. }
  115.  
  116.  
  117. /*****************************************************************
  118.  * TAG( rle_row_free )
  119.  * 
  120.  * Free storage allocated by rle_row_alloc().
  121.  * Inputs:
  122.  *     the_hdr:    Header structure as above.
  123.  *    scanp:        Pointer to scanbuf above.
  124.  * Outputs:
  125.  *     Frees storage referenced by scanp and nrawp.
  126.  * Assumptions:
  127.  *     Storage was allocated by rle_row_alloc, or by use of same
  128.  *    algorithm, at least.
  129.  * Algorithm:
  130.  *     free scanp[0] and scanp.
  131.  */
  132. void
  133. rle_row_free( the_hdr, scanp )
  134. rle_hdr *the_hdr;
  135. rle_pixel **scanp;
  136. {
  137.     int i;
  138.  
  139.     if ( the_hdr->alpha )
  140.     scanp--;
  141.     for ( i = 0; i < the_hdr->ncolors + the_hdr->alpha; i++ )
  142.     if ( scanp[i] != 0 )
  143.     {
  144.         free( (char *)scanp[i] );
  145.         break;
  146.     }
  147.     free( (char *)scanp );
  148. }
  149.